
function _chessBoard_blinkSquareCount(square_id, blink_count){
	var square_obj, fun;
	square_obj = document.getElementById(square_id);
	if ((blink_count%2) == 0){
		square_obj.style['borderColor'] = 'black';
	} else {
		square_obj.style['borderColor'] = 'red';
	}
	if (blink_count > 0){
		fun = "_chessBoard_blinkSquareCount('"+square_id+"', "+(blink_count-1)+");";
		setTimeout(fun, 100);
	} else {
		square_obj.style['borderColor'] = 'black';
	}
}

ChessBoard.prototype = new Printer ();

function ChessBoard(io_obj){
	/** HERITAGE */
	this.parent = Printer;
	this.parent (io_obj);

	/** PRIVATE */
	this._io = null;
	this._board = null;

	/** PUBLIC */
	this._constructor = function chessBoard__constructor (io_obj) {
		var i;
		var j;

		this._io = io_obj;
		this._board = new Array ();
		for (i = 0; i < 8; i++){
			this._board[i] = new Array ();
			for (j = 0; j<8; j++){
				this._board[i][j] = null;
			}
		}
		this.reset ();
	};


	this.isPieceAt = function chessBoard_isPieceAt (loc_obj) {
		var result;
		var x,y;
		x = loc_obj.getX();
		y = loc_obj.getY();
		result = (this._board[x][y] != null);
		// alert("Is Piece at ("+x+","+y+")? "+result);
		return result;
	}

	this.delPieceAt = function chessBoard_delPieceAt(loc_obj){
		var x,y;
		var square_id, square_obj;
		x = loc_obj[0];
		y = loc_obj[1];
		this._board[x][y] = null;		

		// on vire tous les fils
		square_id = loc_obj.getSquareId ();
		square_obj = document.getElementById (square_id);

		while(square_obj.childNodes.length > 0){
			square_obj.removeChild(square_obj.firstChild);
		}

	}

	this.setPieceAt = function chessBoard_setPieceAt (piece_obj, loc_obj) {
		var square_id, square_obj;
		var div_obj;
		piece_obj.setLocation(loc_obj);
		this._board[loc_obj.getX()][loc_obj.getY()] = piece_obj;
		// on vire tous les fils
		square_id = loc_obj.getSquareId ();
		square_obj = document.getElementById (square_id);

		while(square_obj.childNodes.length > 0){
			square_obj.removeChild(square_obj.firstChild);
		}
		// on met une Tour
		div_obj = document.createElement("div");
		div_obj.setAttribute("class","piece "+piece_obj.getType());
		square_obj.appendChild(div_obj);
	}

	this.getPieceAt = function chessBoard_getPieceAt (loc_obj) {
		return this._board[loc_obj.getX()][loc_obj.getY()];	
	}

	this.selectSquare = function chessBoard_selectSquare (loc_obj) {
		var square_id, square_obj;
		square_id = loc_obj.getSquareId ();
		square_obj = document.getElementById (square_id);
		square_obj.style['borderColor'] = 'yellow';
	}

	this.unselectSquare = function chessBoard_unselectSquare (loc_obj) {
		var square_id, square_obj;
		square_id = loc_obj.getSquareId ();
		square_obj = document.getElementById(square_id);
		square_obj.style['borderColor'] = 'black';
	}

	this.blinkSquare = function chessBoard_blinkSquare (loc_obj) {
		var square_id, square_obj;
		square_id = loc_obj.getSquareId ();
		_chessBoard_blinkSquareCount(square_id, 10);
	}

	this.makeMove = function chessBoard_makeMove(loc_src_obj, loc_dst_obj) {
		var piece_obj;
		//alert("Move: "+loc_src_obj.getX()+", "+loc_src_obj.getY()
		//	+" -> "+loc_dst_obj.getX()+", "+loc_dst_obj.getY());
		piece_obj = this.getPieceAt(loc_src_obj);
		this.setPieceAt(piece_obj, loc_dst_obj);
		this.delPieceAt(loc_src_obj);
	}
	
	this.reset = function chessBoard_reset(){
		var i, j, loc, piece;
		var color_str;
		for (i = 0; i<8; i++){
			for (j = 0; j<8; j++){
				loc = new Location(i, j);
				this.delPieceAt(loc);
			}
		}

		color_str = "black";

		loc = new Location (0, 0);
		piece = new PieceRook(this._io, color_str);
		piece.setChessBoard (this);
		this.setPieceAt (piece, loc);

		loc = new Location (1, 0);
		piece = new PieceKnight(this._io, color_str);
		piece.setChessBoard (this);
		this.setPieceAt (piece, loc);

		loc = new Location (2, 0);
		piece = new PieceBishop(this._io, color_str);
		piece.setChessBoard (this);
		this.setPieceAt (piece, loc);

		loc = new Location (3, 0);
		piece = new PieceQueen(this._io, color_str);
		piece.setChessBoard (this);
		this.setPieceAt (piece, loc);
		
		loc = new Location (4, 0);
		piece = new PieceKing(this._io, color_str);
		piece.setChessBoard (this);
		this.setPieceAt (piece, loc);

		loc = new Location (5, 0);
		piece = new PieceBishop(this._io, color_str);
		piece.setChessBoard (this);
		this.setPieceAt (piece, loc);

		loc = new Location (6, 0);
		piece = new PieceKnight(this._io, color_str);
		piece.setChessBoard (this);
		this.setPieceAt (piece, loc);

		loc = new Location (7, 0);
		piece = new PieceRook(this._io, color_str);
		piece.setChessBoard (this);
		this.setPieceAt (piece, loc);

		for (j = 0; j < 8; j++){
			loc = new Location (j, 1);
			piece = new PiecePawn(this._io, color_str);
			piece.setChessBoard (this);
			this.setPieceAt (piece, loc);
		}

		color_str = "white";

		loc = new Location (0, 7);
		piece = new PieceRook(this._io, color_str);
		piece.setChessBoard (this);
		this.setPieceAt (piece, loc);

		loc = new Location (1, 7);
		piece = new PieceKnight(this._io, color_str);
		piece.setChessBoard (this);
		this.setPieceAt (piece, loc);

		loc = new Location (2, 7);
		piece = new PieceBishop(this._io, color_str);
		piece.setChessBoard (this);
		this.setPieceAt (piece, loc);

		loc = new Location (3, 7);
		piece = new PieceQueen(this._io, color_str);
		piece.setChessBoard (this);
		this.setPieceAt (piece, loc);

		loc = new Location (4, 7);
		piece = new PieceKing(this._io, color_str);
		piece.setChessBoard (this);
		this.setPieceAt (piece, loc);

		loc = new Location (5, 7);
		piece = new PieceBishop(this._io, color_str);
		piece.setChessBoard (this);
		this.setPieceAt (piece, loc);

		loc = new Location (6, 7);
		piece = new PieceKnight(this._io, color_str);
		piece.setChessBoard (this);
		this.setPieceAt (piece, loc);

		loc = new Location (7, 7);
		piece = new PieceRook(this._io, color_str);
		piece.setChessBoard (this);
		this.setPieceAt (piece, loc);

		for (j = 0; j < 8; j++){
			loc = new Location (j, 6);
			piece = new PiecePawn(this._io, color_str);
			piece.setChessBoard (this);
			this.setPieceAt (piece, loc);
		}

		/*
		   this._board[7][0] = new PieceRook("white");
		   this._board[7][1] = new PieceKnight("white");
		   this._board[7][2] = new PieceBishop("white");
		   this._board[7][3] = new PieceQueen("white");
		   this._board[7][4] = new PieceKing("white");
		   this._board[7][5] = new PieceBishop("white");
		   this._board[7][6] = new PieceKnight("white");
		   this._board[7][7] = new PieceRook("white");

		   for (j = 0; j < 8; j++){
		   this._board[6][j] = new PiecePawn("black");
		   }
		 */
	}

	/** CONSTRUCT ! */
	this._constructor(io_obj);
}
